﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.SceneLab.UI;

namespace RayDen.SceneLab.Controllers.FormControllers
{
    public class RendererFormController : FormControllerBase<RendererForm>
    {


        public RendererFormController(Form owner)
            : base(owner)
        {
        }

        internal async Task<string> Render()
        {
            form.Progress.Maximum = 350;

            return await Task.Run(() =>
                {
                    Random rnd = new Random();
                    for (int i = 0; i < 30 + rnd.Next(30); i++)
                    {
                        form.Canvas.Invoke(new Action<Bitmap>(Draw), form.Canvas.Image);
                        int i1 = i;
                        form.Invoke(new Action(() =>
                            {
                                form.Progress.Increment(i1);
                            }));
                        Thread.Sleep(i * 10);
                    }
                    return string.Empty;
                });
        }

        private void Draw(Bitmap bmp)
        {
            var rnd = new Random();
            var pens = new[]
                {
                    Brushes.Aqua, Brushes.BlueViolet, Brushes.Red, Brushes.Green, Brushes.Gold, Brushes.DeepPink, Brushes.DarkOliveGreen,
                    Brushes.Brown, Brushes.Blue
                };
            using (var g = Graphics.FromImage(bmp))
            {
                g.FillEllipse(pens[rnd.Next(pens.Length - 1)], new Rectangle(rnd.Next(bmp.Width), rnd.Next(bmp.Height), rnd.Next(bmp.Width / 2), rnd.Next(bmp.Height / 2)));
            }
            form.Canvas.Refresh();
        }
    }
}
